//
//  AddressCell.swift
//  Box
//
//  Created by mn on 2025/3/31.
//
import UIKit

class AddressCell : UITableViewCell{
    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.textColor = .fromHex("#292D32")
        label.font = UIFont.systemFont(ofSize: 15,weight: .heavy)
        return label
    }()
    lazy var phoneLabel: UILabel = {
        let label = UILabel()
        label.textColor = .fromHex("#292D32")
        label.font = UIFont.systemFont(ofSize: 15,weight: .heavy)
        return label
    }()
    lazy var addressLabel: UILabel = {
        let label = UILabel()
        label.textColor = .fromHex("#767E88")
        label.font = UIFont.systemFont(ofSize: 12.5)
        return label
    }()
    lazy var defaultButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage.addressSelect, for: .selected)
        button.setImage(UIImage.addressNormal, for: .normal)
        button.setTitle("  默认地址", for: .normal)
        button.setTitleColor(.fromHex("#292D32"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        return button
    }()
    lazy var editButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage.addressEdit, for: .normal)
        button.addAction { [weak self] in
            let vc = EditAddressViewController(data: self?.data)
            self?.findViewController()?.navigationController?.push(vc)
        }
        return button
    }()
    lazy var deleteButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setImage(UIImage.addressDelete, for: .normal)
        return button
    }()
    let bg = UIView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.backgroundColor = .fromHex("#F4F6F8")
        bg.backgroundColor = .white
        contentView.addSubview(bg)
        [nameLabel,phoneLabel,addressLabel,defaultButton,deleteButton,editButton].forEach {
            bg.addSubview($0)
        }
        separatorInset = UIEdgeInsets(top: 0, left: 10000, bottom: 0, right: 0)
        bg.snp.makeConstraints { make in
            make.edges.equalTo(UIEdgeInsets(top: 13, left: 16, bottom: 0, right: 16))
        }
        nameLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(11.5)
            make.top.equalToSuperview().offset(15.5)
            make.height.equalTo(21)
        }
        phoneLabel.snp.makeConstraints { make in
            make.left.equalTo(nameLabel.snp.right).offset(21)
            make.centerY.equalTo(nameLabel.snp.centerY)
        }
        addressLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(11.5)
            make.top.equalTo(nameLabel.snp.bottom).offset(12)
            make.height.equalTo(18)
        }
        let line = UIView()
        line.backgroundColor = .fromHex("#AAB3BF").withAlphaComponent(0.23)
        bg.addSubview(line)
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(11.5)
            make.top.equalTo(addressLabel.snp.bottom).offset(17)
            make.height.equalTo(1)
        }
        
        defaultButton.snp.makeConstraints { make in
            make.left.equalToSuperview().inset(11.5)
            make.top.equalTo(line.snp.bottom).offset(15)
            make.height.equalTo(20)
        }
        editButton.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalTo(defaultButton.snp.centerY)
        }
        deleteButton.snp.makeConstraints { make in
            make.right.equalTo(editButton.snp.left).offset(-14)
            make.centerY.equalTo(defaultButton.snp.centerY)
        }
    }
    var data: AddressData!{
        didSet{
            defaultButton.isSelected = data.isDefault
            nameLabel.text = data.name
            phoneLabel.text = data.mobile
            addressLabel.text = data.addressPathDes + data.detailAddress
        }
    }
}
